#!/bin/bash
BASE_HOME=$(dirname "$0")
source ${BASE_HOME}/common_setup  # Loading the common code section from 'common_setup', and 'common_setup' in turn loads the shared functions from 'common_functions'.

log_info "Keepalived into master status. If remote host is down, set local host as master role."


# Create binlog parse to SQL work dir if not exists                                             
[[ ! -d ${sqlpath} ]] && mkdir -p ${sqlpath}

## Check if VIP exists on local host 
check_vip_existence

# Check if the remote host is alive
check_remote_host_alive() {
 
							 
    ping -c ${PINGDOWN_C} ${REMOTE_HOST} 
    if (( $? == 0 )); then
        return 1
    else
        return 0
    fi
}

# Continuous check on remote host
log_info "Starting ping remote host by \"ping -c ${PINGDOWN_C} ${REMOTE_HOST}\" "
   
		   
for (( i=1; i<=${PINGDOWN_TOTAL}; i++ ))
do
    check_remote_host_alive
    result_ping=$?
    if [ ${result_ping} -eq 1 ]; then
        log_info "Remote host ${REMOTE_HOST} is alive after ${i} attempts, stopping checks"
        down_time=1
        break
    else
        log_info "Remote host ${REMOTE_HOST} is down after ${i} attempts"
							   
        down_time=0
    fi
done
log_info "End of ping check on remote host ${REMOTE_HOST}"

# If remote host is alive, do not perform any operations
if [ ${down_time} -eq 1 ]; then
    log_info "Remote host ${REMOTE_HOST} is alive, no operations will be performed on local MySQL database"
else
    log_warn "Remote host ${REMOTE_HOST} is down, setting local MySQL database to master role"
    log_info "Checking local host VIP status"
    ps -ef | grep -i keepalived | grep -v grep
    ip addr
    get_slave_delay_status
    
    SQL_STATUS=$(grep -w "Slave_SQL_Running" ${sqlpath}/tmp_local_slave.txt | awk -F": " '{print $2}')
    if [ "${SQL_STATUS}" == "Yes" ]; then
        log_info "Slave SQL thread running status: ${SQL_STATUS}"
    elif [ -n "${SQL_STATUS}" ]; then
        log_info "Slave SQL thread running status: ${SQL_STATUS}, starting SQL thread"
        start_sql_thread    # function in common_functions					  
    fi
    
    if [ -n "${SQL_STATUS}" ]; then
        wait_until_sql_thread_catches_up    # function in common_functions
    else
        log_info "Local MySQL is not configured as slave, continuing execution"																	       
    fi

    log_info "Starting to set local MySQL database to master role"
    log_info "CMD: show master status,set global super_read_only = OFF;set global read_only = OFF,stop slave, reset slave all"											       
    ${MYSQL_LOCAL_CMD} -e "show master status \G"
    ${MYSQL_LOCAL_CMD} -N -L -s -e "set global super_read_only = OFF;set global read_only = OFF;show variables like 'read_only'; show variables like 'super_read_only';"
    ${MYSQL_LOCAL_CMD} -e "stop slave; reset slave all;"
    log_info "The process of setting the local MySQL database to the master role has been completed"
fi

##record the VIP is in local host 
check_vip_existence
log_info "Keepalived end master status and exit scripts running"